import pandas as pd
import numpy as np
import os
import sys
import model_code.gen_data as gd
import model_code.rfg as rfg
import model_code.per_mea as pm
from tensorflow import keras

source = sys.argv[1] #tongji/vital
pre_time = sys.argv[2]#5 10 15
ioh_time = sys.argv[3] #1
ob_win = sys.argv[4] #5 10 15

pre_time = int(pre_time)
pre_time = pre_time / 5
ob_win = int(ob_win)

d_path = source + "/dynamic_normalization/" + ioh_time + "-bt.csv"
#tongji/dynamic_normalization/1-bt.csv"
c_path = "config_bt.json"

static, dynamic, label = gd.gen_data(source, d_path, c_path, pre_time, ob_win)

r = label.sum()
l = label.shape[0] - r
r = l / r
if r < 1:
    r = 1
l = 1
cw = {0: l, 1: r}
print("cw: 1, " + str(r))

model_path = "models/" + source + "-" + str(pre_time) + "-" + ioh_time + "-" + str(ob_win) + ".h5"
#model_path = "models/tongji-1.0-1-5.h5"

model = rfg.create_model_2(dynamic.shape[1:], ob_win)

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', pm.AUC])

dynamic_dim = dynamic.reshape(dynamic.shape[0], dynamic.shape[1], dynamic.shape[2], 1)

history = model.fit([dynamic_dim, dynamic], label, epochs=200, batch_size=1024,class_weight=cw,
                    validation_split=0.3, verbose=2,
                    callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='min'),
                               keras.callbacks.ModelCheckpoint(model_path, monitor='val_loss', save_best_only=True, mode='min', verbose=0)])
